#include "StringTools.h"
#include "Tools.h"


int StringTools_FindSubString(char *pSrc, int nSrcLen, char *pSub, int nSubLen)
{
	int i;
	int j;
	int nPos;
	
	nPos = -1;
	
	//find substr pos
	for(i=0; i<nSrcLen; i++)
	{
		if(pSrc[i]==pSub[0])
		{
			nPos = i;
			for(j=0; j<nSubLen; j++)
			{
				if(pSrc[i+j]!=pSub[j])
				{
					nPos = -1;
					break;
				}
			}
			if(nPos!=-1)
			{
				break;
			}
		}
	}
	return nPos;
}

void StringTools_GetRightString(char *pSrc, char *pDes, char *pStart, char *pEnd)
{
	int nStartPos;
	int nEndPos;
	
	int nSrcLen;
	int nStartLen;
	int nEndLen;
	
	nSrcLen = strlen(pSrc);
	nStartLen = strlen(pStart);
	nEndLen = strlen(pEnd);
	
	nStartPos = -1;
	nEndPos = -1;
	
	nStartPos = StringTools_FindSubString(pSrc, nSrcLen, pStart, nStartLen);
	
	if(nStartPos<0)
	{
		//not found start pos
		return;
	}
	
	//向后偏移过起始字符串
	nStartPos += nStartLen;
	
	if(!pEnd)
	{
		//end is null, copy start pos data to dec buf
		memcpy(pDes, pSrc+nStartPos, nSrcLen-nStartPos);
		return;
	}
	
	nEndPos = StringTools_FindSubString(pSrc+nStartPos, nSrcLen, pEnd, nEndLen);
	
	if(nEndPos<0)
	{
		//not found end pos
		return;
	}
	
	memcpy(pDes, pSrc+nStartPos, nEndPos);
}

/**
 * 功能:
 *		查找最后一个字符位置
 * 参数:
 *		char            *pSrc       源字符串
 *		int             nLen        源字符的长度
 *		char			c			要查找的字符
 * 返回值:
 *		int				最后一次出现c的位置，未找到返回-1
 */
int StringTools_FindLastChar(char *pSrc, int aLen, char c)
{
	int i;
	int r = -1;
	for(i=aLen-1; i>=0; i--)
	{
		if(pSrc[i]==c)
		{
			r = i;
			break;
		}
	}
	return r;
}



/**
 * 功能:
 *		替换字符串中的字符为另一个字符
 * 参数:
 *		char            *pSrc       被替换字符串
 *		int             nLen        被替换字符的长度
 *		char			cSrc        被替换的字符 
 *		char			cDest       用来做替换的字符
 * 返回值:
 *		-
 */
void StringTools_Replace( char *pSrc,int nLen,char cSrc,char cDest )
{
	char *p = pSrc;
	for (int i = 0;i <nLen;i ++,p++)
	{
		if (p && *p  == cSrc)
		{
			*p = cDest;
		}
		if (!p)
		{
			break;
		}
	}
}

void StringTools_DeleteString( char *pSrc,int nLen,char *cSrc)
{
	char *p = pSrc;
	for (int i = 0;i <nLen;i ++)
	{
		if (p && *p  == *cSrc)
		{
			int b = 1;
			char *pcSrc = cSrc;
			char *pp= p;
			int sl = strlen(pcSrc);
			int j=0;
			while(pp && pcSrc && j<sl)
			{
				if(*pp!=*pcSrc)
				{
					b = 0;
					break;
				}
				pp++;
				pcSrc++;
				j++;
			}
			if(b)
			{
				memcpy(p, p+strlen(cSrc), strlen(p)-sl);
				p[strlen(p)-sl]=0;
				if(*p  == *cSrc)
				{
					continue;
				}
			}
			//*p = cDest;
			if (sl == j) {
				nLen-=sl;
			}
			
		}
		p++;
		if (!p)
		{
			break;
		}
	}
}

void StringTools_FilterChineseString( char *pSrc,int nLen)
{
	char c;
	
	for (int i=0; i<nLen; i++) {
		c=pSrc[i];
		if ((c & MASK3BYTES) == MASK3BYTES)
		{
			memcpy(pSrc+i,pSrc+i+3,nLen - i - 3);
			i--;
			nLen -= 3;
			pSrc[nLen] = '\0';
		}
		// 110xxxxx 10xxxxxx
		else if ((c & MASK2BYTES) == MASK2BYTES)
		{
			i+=1;
		}
		// 0xxxxxxx
		else if ((u_byte)c < MASKBYTE)
		{
		}
		
	}
	
}

int StringTools_SubStrCount(char *strbuf, char *ssub)
{
    char *p;
    char subLen;
    int nCount;
    
    nCount = 0;
    subLen = strlen(ssub);
    
    while((p=strstr(strbuf, ssub))!=NULL)
    {
        nCount++;
        strbuf = p+subLen;
    }
    
    return nCount;
}

char* StringTools_ReplaceSubStr(char *strbuf, char *sstr, char *dstr)
{  
    char *p;
    int nPos;
    int nLen;
    char *strDbuf;
    
    int subCount;
    int size;
    
    if ((strbuf == NULL)||(sstr == NULL)||(dstr == NULL))
        return NULL;
    
    nPos = 0;
    nLen = strlen(sstr);
    
    //被替换子串在父串中出现在次数
    subCount = StringTools_SubStrCount(strbuf, sstr);
    //源字符长度-（被替换串长度＊被替换个数）+（替换串长度＊替换个数）+1
    size = strlen(strbuf)-(subCount*nLen)+(subCount*strlen(dstr))+1;
    strDbuf = Tools_Malloc(size);
	
    while((p = strstr(strbuf, sstr))!=NULL)
    {
        strncpy(strDbuf+strlen(strDbuf), strbuf, p-strbuf);
        strcat(strDbuf, dstr);
        nPos = p-strbuf+nLen;
        strbuf+=nPos;
    }
    strcat(strDbuf+strlen(strDbuf), strbuf);
    
    return strDbuf;
}

